function redirect(url){
	window.location = url;
}

//LANGS

//Changement de langue
function admin_switchlang(o){
	var lang = o.getAttribute('rel');
	redirect("http://"+document.domain+"/"+admin_directory+"/lang/"+lang+".html");
}

//Show lang choice
var langChoiceState = "hidden";

function admin_changelang(show){
	if(show && langChoiceState == "hidden"){
		langChoiceState = "show";
		document.getElementById('changelang_box').style.display = "block";
	}else{
		langChoiceState = "hidden";
		setTimeout(admin_hideLangChoice,2000);
	}
}

function admin_hideLangChoice(){
	if(langChoiceState == "hidden")
		document.getElementById('changelang_box').style.display = "none";
}

// ACCOUNT MENU

function admin_disconnect(){
	redirect("http://"+document.domain+"/"+admin_directory+"/disconnect.html");
}


//COMMON

/* AJAX */
function ajax(url,getOrPost,postVar){
	var xhr; 
	try{xhr = new ActiveXObject('Msxml2.XMLHTTP');}
	catch(e){
		try{xhr = new ActiveXObject('Microsoft.XMLHTTP');}
		catch(e2){
			try{xhr = new XMLHttpRequest();}
			catch(e3){xhr = false;}
		}
	}
	
	switch(getOrPost){
	    default:
	    case "get":
		xhr.open("GET", url, false);
		xhr.send(null);
		break;
	    
	    case "post":
		xhr.open("POST", url, false);
		var postSend = null;
		for(var key in postVar){
		    (postSend == null) ? postSend = "" : postSend += "&";
		    postSend += key+"="+postVar[key];
		}
		if(postSend != null){
			var nav = navigator.appVersion;
			var ischrome = nav.indexOf("Chrome") ? true : false;
			    
			xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
			if(!ischrome){//AppleWebkit refused to set unsafe header Content-Length and Connection
				xhr.setRequestHeader("Content-length", postSend.length);
				xhr.setRequestHeader("Connection", "close");
			}
		}
		xhr.send(encodeURI(postSend));
		break;
	}

	if(xhr.readyState  == 4){
		if(xhr.status  == 200) 
			return(xhr.responseText); 
		else 
			return(false);
	}else
		return(false);	
}

/* Resizer */

//Content resizeable panel
var mouseCurrentlyUp = true;
var mouseX;
var mouseY;

document.onmousemove = function(e){
	mouseX = e.clientX;
	mouseY = e.clientY;
	if(!mouseCurrentlyUp){
		var sel = window.getSelection();
		sel.removeAllRanges();
	}
}

document.onmouseup = function(){
	mouseCurrentlyUp = true;
}

function startResize(){
	mouseCurrentlyUp = false;
	resizePanel();
}


function resizePanel(){	
	//Position X de la souris
	var sourisX = mouseX;
	var detect = navigator.userAgent.toLowerCase();
	if((detect.indexOf('msie') + 1)){
	    //largeur document - padding
	    var widthClient = (document.body.clientWidth)-20;
	}else{
	    //largeur document - padding
	    var widthClient = (window.innerWidth)-20; 
	}
	
	
	if((sourisX > 130)&&(sourisX < (widthClient-900))){
	    
	    var leftWidth = parseInt(((sourisX+5)*95)/widthClient); //width in percent
	    var rightWidth = parseInt(95-leftWidth);
	    
	    document.getElementById('resizeable_panel_left').style.width = leftWidth+"%";
	    document.getElementById('resizeable_panel_right').style.width = rightWidth+"%";
	}
	
	
	if(!mouseCurrentlyUp)
	    setTimeout(function(){resizePanel();}, 50);
	
}


//NOTIFICATIONS

//ok/validation
function alertOK(text){
    var o = document.getElementById('ok_jsblock');
    o.innerHTML = text;
    o.style.display = "block";
    o.style.opacity=1;
    o.style.MozOpacity=1;
    o.style.KhtmlOpacity=1;
    o.style.filter="alpha(opacity=100)";
    setTimeout(function(){fadeOut('ok_jsblock',20,0)},3000);
}

//Error
function alertERROR(text){
    var o = document.getElementById('error_jsblock');
    o.innerHTML = text;
    o.style.display = "block";
    o.style.opacity=1;
    o.style.MozOpacity=1;
    o.style.KhtmlOpacity=1;
    o.style.filter="alpha(opacity=100)";
    setTimeout(function(){fadeOut('error_jsblock',20,0)},3000);
}

//Warning
function alertWARN(text){
    var o = document.getElementById('warn_jsblock');
    o.innerHTML = text;
    o.style.display = "block";
    o.style.opacity=1;
    o.style.MozOpacity=1;
    o.style.KhtmlOpacity=1;
    o.style.filter="alpha(opacity=100)";
    setTimeout(function(){fadeOut('warn_jsblock',20,0)},3000);
}

//info
function alertINFO(text){
    var o = document.getElementById('info_jsblock');
    o.innerHTML = text;
    o.style.display = "block";
    o.style.opacity=1;
    o.style.MozOpacity=1;
    o.style.KhtmlOpacity=1;
    o.style.filter="alpha(opacity=100)";
    setTimeout(function(){fadeOut('info_jsblock',20,0)},3000);
}

//Loading
function startLOADING(text){
    var o = document.getElementById('loading_jsblock');
    o.innerHTML = text;
    o.style.display = "block";
    o.style.opacity=1;
    o.style.MozOpacity=1;
    o.style.KhtmlOpacity=1;
    o.style.filter="alpha(opacity=100)";
}

function endLOADING(hideDirectly){
    if(hideDirectly)
	fadeOut('loading_jsblock',1,0);
    else
	fadeOut('loading_jsblock',20,0);
}

//Question
function startQUESTION(text){
    var o = document.getElementById('question_jsblock');
    o.innerHTML = text;
    o.style.display = "block";
    o.style.opacity=1;
    o.style.MozOpacity=1;
    o.style.KhtmlOpacity=1;
    o.style.filter="alpha(opacity=100)";
}

function endQUESTION(hideDirectly){
    if(hideDirectly)
	fadeOut('question_jsblock',1,0);
    else
	fadeOut('question_jsblock',20,0);
}

//FADE OUT block (used by notifications block)
function fadeOut(block, cycle, current){
    var o = document.getElementById(block);
    
    if(cycle == current){
        o.style.display = "none";
    }else{
        var opacity = parseInt((100/cycle)*(cycle-current));
        
        o.style.opacity=(opacity/100);
        o.style.MozOpacity=(opacity/100);
        o.style.KhtmlOpacity=(opacity/100);
        o.style.filter="alpha(opacity="+opacity+")";
        
        current++;
        
        setTimeout("fadeOut('"+block+"',"+cycle+","+current+")",50);
    }
}

//DEBUG

/**
 * Function : dump()
 * Arguments: The data - array,hash(associative array),object
 *    The level - OPTIONAL
 * Returns  : The textual representation of the array.
 */
function dump(arr,level) {
	var dumped_text = "";
	if(!level) level = 0;
	
	//The padding given at the beginning of the line.
	var level_padding = "";
	for(var j=0;j<level+1;j++) level_padding += "    ";
	
	if(typeof(arr) == 'object') { //Array/Hashes/Objects 
		for(var item in arr) {
			var value = arr[item];
			
			if(typeof(value) == 'object') { //If it is an array,
				dumped_text += level_padding + "'" + item + "' ...\n";
				dumped_text += dump(value,level+1);
			} else {
				dumped_text += level_padding + "'" + item + "' => \"" + value + "\"\n";
			}
		}
	} else { //Stings/Chars/Numbers etc.
		dumped_text = "===>"+arr+"<===("+typeof(arr)+")";
	}
	return dumped_text;
}